package com.gohome.gohomechat.ui.adapter;

import android.os.Message;
import android.text.SpannableString;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.easemob.chat.EMContactManager;
import com.easemob.chat.EMConversation;
import com.easemob.chat.EMMessage;
import com.gohome.gohomechat.R;
import com.gohome.gohomechat.utils.FaceConversionUtil;
import com.gohome.gohomechat.utils.TextUtil;
import com.gohome.gohomechat.utils.TimeStampUtils;

import java.util.List;

/**
 * Created by dllo on 16/3/5.
 */
public class SessionListViewAdapter extends BaseAdapter {

    private List<EMConversation> datas;

    public void addData(List<EMConversation> datas) {
        this.datas = datas;
        notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        return datas != null && datas.size() > 0 ? datas.size() : 0;
    }

    @Override
    public Object getItem(int position) {
        return datas != null && datas.size() > 0 ? datas.get(position) : null;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (convertView == null) {
            convertView = LayoutInflater.from(parent.getContext()).inflate(R.layout.session_listview_item, parent, false);
            holder = new ViewHolder(convertView);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        EMConversation conversation = datas.get(position);
        EMMessage message = conversation.getLastMessage();
        holder.session_listview_item_username.setText(conversation.getUserName());
        if (conversation.getUnreadMsgCount() == 0)
            holder.session_listview_unreadmessage.setVisibility(View.GONE);
        else
            holder.session_listview_unreadmessage.setText(conversation.getUnreadMsgCount()+"");
        if (message.getType() == EMMessage.Type.TXT) {
            SpannableString spannableString = FaceConversionUtil.getInstace().getExpressionString(parent.getContext(), TextUtil.splitTxt(message.getBody().toString()));
            holder.session_listview_item_message.setText(spannableString);
        } else if (message.getType() == EMMessage.Type.IMAGE) {
            holder.session_listview_item_message.setText("[图片]");
        } else if (message.getType() == EMMessage.Type.LOCATION) {
            holder.session_listview_item_message.setText("[地理位置]");
        } else if (message.getType() == EMMessage.Type.VIDEO) {
            holder.session_listview_item_message.setText("[视频]");
        } else if (message.getType() == EMMessage.Type.VOICE) {
            holder.session_listview_item_message.setText("[语音]");
        } else if (message.getType() == EMMessage.Type.FILE) {
            holder.session_listview_item_message.setText("[文件]");
        } else if (message.getType() == EMMessage.Type.CMD) {
        }
        long time = message.getMsgTime() / 1000 - TimeStampUtils.getTimesmorning();
        Log.i("lanou", "------------>" + time + "   " + TimeStampUtils.getTimesmorning());
        if ((time > 0 || time == 0) && time < 86400) {
            holder.session_listview_item_time.setText("今天" + TimeStampUtils.stampToDate2(message.getMsgTime()));
        } else if (time < 0 && ((time + 86400) > 0 || (time + 86400) == 0)) {
            holder.session_listview_item_time.setText("昨天" + TimeStampUtils.stampToDate2(message.getMsgTime()));
        } else {
            holder.session_listview_item_time.setText(TimeStampUtils.stampToDate(message.getMsgTime()));
        }

        return convertView;
    }

    class ViewHolder {
        private ImageView session_listview_item_head;
        private TextView session_listview_item_username, session_listview_item_message, session_listview_unreadmessage, session_listview_item_time;

        public ViewHolder(View view) {
            session_listview_item_head = (ImageView) view.findViewById(R.id.session_listview_item_head);
            session_listview_item_username = (TextView) view.findViewById(R.id.session_listview_item_username);
            session_listview_item_message = (TextView) view.findViewById(R.id.session_listview_item_message);
            session_listview_unreadmessage = (TextView) view.findViewById(R.id.session_listview_unreadmessage);
            session_listview_item_time = (TextView) view.findViewById(R.id.session_listview_item_time);
        }
    }
}
